package com.weaver.model;

import java.io.*;
import java.util.*;

/**
 * Dictionary class - Loads and manages the word dictionary
 */
public class Dictionary {
    private Set<String> words;
    private Map<Integer, List<String>> wordsByLength;
    
    /**
     * Constructor
     * Loads the dictionary file
     */
    public Dictionary() {
        words = new HashSet<>();
        wordsByLength = new HashMap<>();
        loadDictionary();
    }
    
    /**
     * Load the dictionary file
     */
    private void loadDictionary() {
        try {
            // Try to load dictionary file from resources directory
            InputStream inputStream = getClass().getResourceAsStream("/dictionary.txt");
            if (inputStream == null) {
                // If not found in resources directory, try to load from file system
                File file = new File("dictionary.txt");
                if (file.exists()) {
                    inputStream = new FileInputStream(file);
                } else {
                    System.err.println("Dictionary file not found");
                    return;
                }
            }
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                // Convert to uppercase and trim whitespace
                String word = line.trim().toUpperCase();
                if (!word.isEmpty()) {
                    words.add(word);
                    
                    // Categorize by length
                    int length = word.length();
                    if (!wordsByLength.containsKey(length)) {
                        wordsByLength.put(length, new ArrayList<>());
                    }
                    wordsByLength.get(length).add(word);
                }
            }
            reader.close();
            
            System.out.println("Loaded " + words.size() + " words");
        } catch (IOException e) {
            System.err.println("Error loading dictionary file: " + e.getMessage());
        }
    }
    
    /**
     * Check if a word is valid
     * @param word the word to check
     * @return true if the word is in the dictionary, false otherwise
     */
    public boolean isValidWord(String word) {
        return words.contains(word.toUpperCase());
    }
    
    /**
     * Get all words of a specified length
     * @param length word length
     * @return list of words of the specified length
     */
    public List<String> getWordsByLength(int length) {
        if (wordsByLength.containsKey(length)) {
            return new ArrayList<>(wordsByLength.get(length));
        }
        return new ArrayList<>();
    }
    
    /**
     * Check if the dictionary is empty
     * @return true if the dictionary is empty, false otherwise
     */
    public boolean isEmpty() {
        return words.isEmpty();
    }
    
    /**
     * Get the number of words in the dictionary
     * @return number of words
     */
    public int size() {
        return words.size();
    }
}